package com.perceptivesoftware.licensing.client.dialogs.useradmin;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.perceptivesoftware.licensing.client.dialogs.useradmin.callbackhandlers.SaveDiscardButtonCallbackHandler;

public class MainSecurityManagerPanel extends Composite {

	SaveDiscardButtonCallbackHandler sdbch;
	LayoutContainer outerLayoutContainer = new LayoutContainer();
	ContentPanel innerContentPanel;
	BreadCrumbs bc = new BreadCrumbs();
	Button saveButton = new Button("Save");
	Button discardButton = new Button("Cancel");

	MainSecurityManagerPanel(final SaveDiscardButtonCallbackHandler aSdbch) {
		initComponent(outerLayoutContainer);
		outerLayoutContainer.setLayout(new FitLayout());
		sdbch = aSdbch;
		saveButton.setIconStyle("perceptive-icon-save");
		saveButton.addListener(Events.Select, new Listener<ButtonEvent>() {

			public void handleEvent(final ButtonEvent be) {
				sdbch.saveButtonClicked();
			}
		});

		discardButton.setIconStyle("perceptive-icon-close");
		discardButton.addListener(Events.Select, new Listener<ButtonEvent>() {

			public void handleEvent(final ButtonEvent be) {
				sdbch.discardButtonClicked();
			}
		});

	}

	public void addButtons() {
		innerContentPanel.getButtonBar().setAlignment(HorizontalAlignment.CENTER);
		innerContentPanel.addButton(saveButton);
		innerContentPanel.addButton(discardButton);
		innerContentPanel.getButtonBar().setSpacing(10);
		disableSaveDiscardButtons();
	}

	public void setBreadcrumbs(final SecurityManagerNavModel anm, final TreeStore<SecurityManagerNavModel> ts) {
		bc.setBreadcrumbs(anm, ts);
	}

	public void setCurrentComposite(final SecurityManagerView view, final Boolean saveable) {
		if (innerContentPanel != null) {
			innerContentPanel.removeAll();
			innerContentPanel.getButtonBar().removeAll();
		}
		outerLayoutContainer.removeAll();
		innerContentPanel = new ContentPanel();
		outerLayoutContainer.add(innerContentPanel);
		innerContentPanel.setLayout(new BorderLayout());
		innerContentPanel.setHeading("Configuration");
		final BorderLayoutData northBorderLayoutData = new BorderLayoutData(LayoutRegion.NORTH, 23);
		northBorderLayoutData.setMargins(new Margins(0, 0, 5, 0));
		innerContentPanel.add(bc, northBorderLayoutData);
		if (view == null) {
			innerContentPanel.add(new LayoutContainer(), new BorderLayoutData(LayoutRegion.CENTER));
		}
		else {
			innerContentPanel.add(view, new BorderLayoutData(LayoutRegion.CENTER));
			innerContentPanel.setHeading(view.getName());
			if (saveable) {
				addButtons();
			}
		}
		innerContentPanel.layout();
		outerLayoutContainer.layout();
	}

	public void enableSaveDiscardButtons() {
		saveButton.enable();
		discardButton.enable();
	}

	public void disableSaveDiscardButtons() {
		saveButton.disable();
		discardButton.disable();
	}
}
